const db = wx.cloud.database();
const _ = db.command;
const $ = db.command.aggregate
const app = getApp();
const common = require("../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderid:null,
    orderObj:null,
    createTime:"",
    approveTime:"",
    completeTime:"",
    role:null,
    timer:null,
    rate1: 0,
    rate2: 0,
    rate3: 0,
    totalRate: 0,
  },
  onChange1(event) {
    this.setData({
      rate1: event.detail,
    });
  },
  onChange2(event) {
    this.setData({
      rate2: event.detail,
    });
  },
  onChange3(event) {
    this.setData({
      rate3: event.detail,
    });
  },
  onChange4(event) {
    this.setData({
      totalRate: event.detail,
    });
  },
  getOrder(){
    wx.showLoading({
      title: '正在加载',
    })
    db.collection("orderList").doc(this.data.orderid).get().then(res=>{
      this.setData({
        orderObj:res.data
      })
      this.setData({
        createTime:app.formateTime(res.data.createTime),
        approveTime:app.formateTime(res.data.approveTime),
        completeTime:app.formateTime(res.data.completeTime),
      });
      wx.hideLoading()
    })
  },
  async evaluate(res){
    let detail = res.detail.value;
    if (detail.rate1==0 || detail.rate2==0 || detail.rate3==0 || detail.totalRate==0 ) {
      wx.showToast({
        title: '请完成评分',
        icon:'none'
      })
    }
    let finalRate = ((Number(detail.rate1)+Number(detail.rate2)+Number(detail.rate3))/3*0.4+detail.totalRate*0.6).toFixed(1);
    await db.collection("orderList").doc(this.data.orderid).update({
      data:{
        status:4,
        statusLabel:app.globalData.statusList[4],
        comment:detail.comment,
        rate1: detail.rate1,
        rate2: detail.rate2,
        rate3: detail.rate3,
        totalRate: detail.totalRate,
        finalRate:Number(finalRate)
      }
    }).then(res=>{
      if (res.stats.updated>0) {
        this.setData({
          timer:null
        })
        wx.redirectTo({
          url: '/pages/success/success?type=evaluate',
        })
      }
    })
    this.countWorkerRate();
  },
  // 计算维修员的综合评分及个人积分
  async countWorkerRate(){
    let workerNum=this.data.orderObj.workerNum;
    let result = null;
    await db.collection('orderList').aggregate()
    .match({
      workerNum:workerNum,
      status: 4
    })
    .group({
      _id: null,
      integral: $.sum('$finalRate'),
      score: $.avg('$finalRate'),
    })
    .end().then(res => {
      result = res.list[0];
    })
    wx.cloud.callFunction({
      name: "updateWorkerScore",
      data: {
        num: workerNum,
        obj: {
          integral:result.integral.toFixed(0),
          score:result.score.toFixed(1)
        }
      }
    }).then(res=>{})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orderid=options.id;
    this.setData({
      orderid:orderid,
      role:app.globalData.role,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrder();
    // 取消计时器
    // console.log("停止计时器");
    // this.setData({
    //   timer:common.cancelTimer(this.data.orderid)
    // })
    // console.log(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // if (this.data.timer) {
    //   console.log("启动计时器");
    //   common.AutoEvaluate(this.data.orderid);
    // }
  },  

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})